<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('项目最新收款报表') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('款项名称')" prop="collectionName">
        <el-input
          v-model="queryParams.collectionName"
          :placeholder="$t('款项名称')"
          clearable
          size="small"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('项目名称')" prop="dealName">
        <el-input
          v-model="queryParams.dealName"
          :placeholder="$t('项目名称')"
          clearable
          size="small"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('公司名称')" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          :placeholder="$t('公司名称')"
          clearable
          size="small"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('项目经理姓名')" prop="projectManagerName">
        <el-input
          v-model="queryParams.projectManagerName"
          :placeholder="$t('项目经理姓名')"
          clearable
          size="small"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          v-hasPermi="['deal:collection:export']"-->
      <!--          type="warning"-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="exportHandle"-->
      <!--        >{{ $t("导出") }}</el-button>-->
      <!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="dealCollectionList" border max-height="600">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column min-width="180" :label="$t('项目名称')" align="center" prop="dealName" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="link-type" @click="jumpHome(scope.row)">{{ scope.row.dealName }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="180" :label="$t('公司名称')" align="center" prop="companyName" show-overflow-tooltip />
      <el-table-column min-width="180" :label="$t('项目经理')" align="center" prop="projectManagerName" show-overflow-tooltip />
      <el-table-column min-width="180" :label="$t('最新收款名称')" align="center" prop="collectionName" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.collectionName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('最新收款金额') + $t('金额单位')" align="center" prop="collectionMoney" width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.collectionMoney, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('最新收款时间')" align="center" prop="collectionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.collectionTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目收款对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealCollectionForm ref="dealCollectionForm" />
    </el-dialog>
  </div>
</template>

<script>
import dealCollectionForm from './dealCollectionForm.vue'
import { delDealCollection, getReportList } from '@/api/deal/collection/dealCollection'
import { selectContractToAnxin } from '@/api/oa/contract/oaContract'
export default {
  components: {
      dealCollectionForm
  },
  data() {
      return {
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 项目收款表格数据
        dealCollectionList: [],
        // 查询 收款时间
        collectionTimes: [],
        // 弹出层标题
        title: '项目收款',
        // 是否显示弹出层
        formOpen: false,
        // 日期范围
        dateRange: [],
        // 是否开具发票字典
        invoiceOptions: [],
        // 收款状态字典
        collectionStateOptions: [],
          // 查询参数
          queryParams: {
              dealId: this.$route.query.id,
              collectionName: undefined,
              joinContract: undefined,
              collectionTime: undefined,
              collectionState: undefined,
              startDate: undefined,
              endDate: undefined,
              pageNum: 1,
              pageSize: 10,
              projectManagerName: undefined,
            companyName: undefined
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {},
          // 合同下拉
          contractOptions: [],
        // 团队成员项目ID参数
          dealTeamDealId: undefined
      }
  },
  created() {
    this.getList()
    this.getDicts('invoice_yn').then(response => {
        this.invoiceOptions = response.data
    })
    this.getDicts('collection').then(response => {
        this.collectionStateOptions = response.data
    })
    // 合同
    selectContractToAnxin({ dealId: this.$route.query.id }).then(response => {
      this.contractOptions = []
      response.data.forEach(item => {
        this.contractOptions.push({ dictValue: item.id, dictLabel: item.name })
      })
    })
  },
  methods: {
    /** 查询项目收款列表 */
    getList() {
      this.loading = true
      if (this.collectionTimes != null && this.collectionTimes.length > 1) {
        this.queryParams.startDate = this.collectionTimes[0]
        this.queryParams.endDate = this.collectionTimes[1]
      } else {
        this.queryParams.startDate = undefined
        this.queryParams.endDate = undefined
      }
      getReportList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.dealCollectionList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 是否开具发票字典翻译
    invoiceFormat(row) {
      return this.selectDictLabel(this.invoiceOptions, row.invoice)
    },
    // 收款状态字典翻译
    collectionStateFormat(row) {
      return this.selectDictLabel(this.collectionStateOptions, row.collectionState)
    },
    formatContract(row) {
      return this.selectDictLabel(this.contractOptions, row.joinContract)
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            dealId: undefined,
            collectionName: undefined,
            joinContract: undefined,
            collectionTime: undefined,
            collectionMoney: undefined,
            invoice: '0',
            collectionState: '0',
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.collectionTimes = []
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealCollectionForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealCollectionForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delDealCollection(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.downLoadExcel('/collection/dealCollection/export', this.queryParams)
    },
    /** 跳转项目详情 */
    jumpHome(row) {
      // 获取项目详情
      if (!row.id) return
      var location = this.$router.resolve({ path: '/deal/home/dealBaseHome', query: { id: row.dealId }})
      window.open(location.href, '_blank')
    },
    // 不同的收款类型返回不同的颜色
    collectionStateColor(item) {
      if (item.collectionState === 'YSK') {
        return { color: 'green' }
      } else if (item.collectionState === 'YWT') {
        return { color: '#FAD033' }
      } else if (item.collectionState === 'HZ') {
        return { color: 'red' }
      } else {
        return ''
      }
    }
  }
}
</script>
